﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using Dapper;using System.Linq;
using Gx.Model.Role;//实体
using Gx.Common;//通用类
using Dapper;

namespace Gx.Dal.Role
{
    /// <summary>
    /// 
    /// </summary>
    public class M_RolePageUrlInfoDal : SqlServerHelper, IDisposable
    {
        public bool DelRolePageUrlInfo(int RoleId)
        {
            string strSql = "DELETE FROM dbo.M_RolePageUrlInfo WHERE RoleId=@RoleId";
            using (SqlConnection conn = ConfigHelper.Open(ConfigHelper.ConnectionStrings))
            {
                return conn.Execute(strSql.ToString(), new { RoleId }) > 0;
            }
        }

        public List<M_RolePageUrlInfo> GetRolePageUrlInfoList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT ");
            strSql.Append(" ID");
            strSql.Append(",RoleID");
            strSql.Append(",PageUrlID");
            strSql.AppendFormat(" FROM M_RolePageUrlInfo WHERE 1=1 {0}", strWhere);
            using (SqlConnection conn = ConfigHelper.Open(ConfigHelper.ConnectionStrings))
            {
                List<M_RolePageUrlInfo> list = conn.Query<M_RolePageUrlInfo>(strSql.ToString()).ToList();
                return list;
            }
        }

        /// <summary>
        /// 添加权限(事务操作)
        /// </summary>
        /// <returns></returns>
        public bool InsertRolePageUrlInfo(DataTable dt)
        {
            string strSql = "M_RolePageUrlInfo";
            using (SqlConnection conn = new SqlConnection(ConfigHelper.ConnectionStrings))
            {
                conn.Open();
                using (SqlBulkCopy sbc = new SqlBulkCopy(conn))
                {
                    sbc.DestinationTableName = strSql;
                    sbc.ColumnMappings.Add("RoleID", "RoleID");
                    sbc.ColumnMappings.Add("PageUrlID", "PageUrlID");
                    sbc.WriteToServer(dt);
                    return true;
                }
            }
        }
    }
}
